<?php
/**
 * TinyMVC - TinyMVCActionController.class.php file
 *
 * @author Adrian Wieczorek <adi(at)tinymvc(dot)org>
 * @package TinyMVC
 * @version $Id: TinyMVCActionController.class.php 3 2007-09-11 12:08:07Z adi3ek $
 *
 */

/**
 * TinyMVCActionController abstract class, module action controllers should inherit from this class, for example:
 * 
 * <code>
 * <?php
 * class FooActionController extends TinyMVCActionController {
 *  
 *  public function barAction() {
 *   // action logic here, this method will be called for request: /index.php?mod=foo&act=bar
 *  }
 * 
 * }
 * ?>
 * </code>
 * 
 * @package TinyMVC
 * 
 */
abstract class TinyMVCActionController {
	/**
		* Transport object for Model and View
		* @var TinyMVCModelAndView
		*/
	protected $modelAndView = null;
	/**
		* Current action name
		* @var string
		*/
	protected $currentAction;
	/**
		* Model (alias: $modelAndView->getModel())
		* @var array
		*/
	protected $model;
	/**
		* View (alias: $modelAndView->getView())
		* @var array
		*/
	protected $view;
	/**
		* Current module name
		* @var string
		*/
	protected $currentModule;		
	/**
	 * Default action name
	 * @var string
	 */
	protected $defaultAction ;
	/**
	 * Request object
	 * @var TinyMVCRequest
	 */
	protected $request = null;

	/**
	 * this constructor need to be called in descendant classes
	 * @param TinyMVCModelAndView $modelAndView model and view transfer object
	 * @param TinyMVCRequest $request	request object
	 * @param string $defaultAction default action name
	 */
	public function __construct(TinyMVCModelAndView $modelAndView, TinyMVCRequest $request, $defaultAction) {
		$this->setModelAndView($modelAndView);
		$this->setRequest($request);
		$this->defaultAction = $defaultAction;
		$this->currentAction = $request->getParam('act') ? $request->getParam('act') : $this->defaultAction;
	}

	/**
		* find and execute correct action method
		* @return TinyMVCModelAndView transfer object
		*/
	public function doAction() {
		$actionMethod = strtolower($this->currentAction)."Action";

		if(method_exists($this, $actionMethod)) {						
			$this->$actionMethod();
			return $this->modelAndView;  
		}
		else {
			throw new TinyMVCException("Unknown action: " . $this->currentAction);
		}
	}
  
	/**
	 * get model and view
	 * @return TinyMVCModelAndView model and view object
	 */
	public function getModelAndView() {
		return $this->modelAndView;	
	}
  
	/**
	 * set model and view object
	 * @param TinyMVCModelAndView $modelAndView model and view transfer object
	 *
	 */
	public function setModelAndView(TinyMVCModelAndView $modelAndView) {
		$this->modelAndView = $modelAndView;	

		// setting aliases
		$this->model = $this->modelAndView->getModel();
		$this->view = $this->modelAndView->getView();
	}
	
	/**
	 * get request object
	 * @return TinyMVCRequest request object
	 */
	public function getRequest() {
		return $this->request;
	}
  
	/**
	 * set request object
	 * @param TinyMVCRequest $request	request object
	 */
	public function setRequest(TinyMVCRequest $request) {
		$this->request = $request;  	
		$this->setCurrentModule($request->getParam("mod"));
	}
  
	/**
	 * get current module name
	 * @return string module name
	 */
	public function getCurrentModule() {
		return $this->currentModule;
	}
  
	/**
	 * set current module name
	 * @param string $moduleName module name
	 */
	public function setCurrentModule($moduleName) {
		$this->currentModule = $moduleName;  	
	}
  
	/**
	 * get default action
	 * @return string action name
	 */
	public function getDefaultAction() {
		return $this->defaultAction;
	}
  
	/**
	 * set default action name
	 * @param string $defaultAction action name 
	 */
	public function setDefaultAction($defaultAction) {
		$this->defaultAction = $defaultAction;
	} 
}
